﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDebug>
#include <QButtonGroup>
#include <QThread>
#include <QIcon>
#include <QLabel>
#include <QPalette>
#include <QCloseEvent>
#include <QMessageBox>
#include <QPropertyAnimation>
#include <QPushButton>
#include <QQueue>
#include <QTextCodec>

#include "maindesigner.h"
#include "dataanalyse.h"
#include "systemsetting.h"
#include "serialservice.h"

#define SSS(R) #R
#define TTT(R) SSS(R)

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    void setupDisplayinterface(void);
    void setupMainStates(void);
    void setupSerialThread(void);
    void showTemporaryMessage(const QString &message, int timeout, const QString &color = "#4a86e8");
    void closeEvent(QCloseEvent *event);

signals:
    void sig_changeSerial(QString port_name,int baud_rate);
    void sig_serialOpen(void);
    void sig_serialClose(void);
    void sig_serialWrite(QByteArray send_data);
    void sig_displayLog(const QByteArray &txt, QColor color);


private slots:
    void hideMessage();

private:
    // 消息数据结构
    struct Message {
        QString message;
        int timeout;
        QString color;
    };
    const QString COLOR_SUCCESS = "#4CAF50";
    const QString COLOR_WARNING = "#FF9800";
    const QString COLOR_ERROR = "#F44336";

    Ui::MainWindow *ui;
    QButtonGroup btn_group;

    MainDesigner *my_maindesigner;
    DataAnalyse *my_dataanalyse;
    SystemSetting *my_systemsetting;

    QLabel *lab_serialState = nullptr;
    QLabel *lab_tempMessage = nullptr;
    QTimer messageTimer;               // 消息显示定时器
    QPropertyAnimation *fadeAnimation; // 淡入淡出动画
    QQueue<Message> messageQueue;      // 消息队列
    QLabel *statusMessageLabel;        // 用于显示消息的标签

    QThread m_serialThread;
    SerialService *m_serialWork = nullptr;
};
#endif // MAINWINDOW_H
